#include "CMainMenuState.h"
#include "CGame.h"
#include "COptionMenuState.h"
#include "CGamePlayState.h"
#include "CXInput.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_FModManager.h"
#include "SGD Wrappers/CSGD_WaveManager.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "CAnimationManager.h"
#include "CExitState.h"
#include "CHowToPlayState.h"
#include "Mail System.h"
#include <ctime>
#include "CUnit.h"
#include "CObjectManager.h"
#include "CObjGenerator.h"
#include "CEventSystem.h"
#include "CIntroState.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "CCreditState.h"

CMainMenuState::CMainMenuState()
{
	Type(MAIN);
	m_bfItem.Init("Ludicrum_Reclamation_Alt.fnt");
	CSGD_Direct3D::GetInstance()->Clear(0,0,0);
	m_bfItem.DrawBitmapText("Loading...", 200, 200, 2.0f);
	m_nBGMusic = CSGD_FModManager::GetInstance()->LoadSound("Resource/Sound/Awakening.mp3");
	m_nBGImage = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/piedpiper2.jpg");

}
CMainMenuState::~CMainMenuState()
{
	//
}
CMainMenuState* CMainMenuState::GetInstance()
{
	static CMainMenuState instance;
	return &instance;
}
bool CMainMenuState::Input()
{
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_ESCAPE))
	{
		CGame::GetInstance()->ChangeState(NULL);
		//return false;
	}

	if(	CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_DOWN) || 
		CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_RIGHT) ||
		((CXInput::GetInstance()->LStickY() <= -7850 ||
		CXInput::GetInstance()->LStickX() >= 7850) && CXInput::GetInstance()->IsLStickMoved() == false))
	{
		m_fIntroCounter = 0;
		CSGD_FModManager::GetInstance()->PlaySound(CGame::GetInstance()->ChoiceSound());
		CSGD_FModManager::GetInstance()->SetVolume(CGame::GetInstance()->ChoiceSound(), CGame::GetInstance()->SFXVol());
		CXInput::GetInstance()->IsLStickMoved(true);
		vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();
		for(unsigned int i = 0; i < m_vMenuItems.size() - 1; i++)
			StuffIter++;
		float fTempX = StuffIter->TarPosX();
		float fTempY = StuffIter->TarPosY();

		while(StuffIter != m_vMenuItems.begin())
		{
			vector<CMenuItem>::iterator temp = StuffIter;

			--StuffIter;

			temp->GoTo(StuffIter->TarPosX(), StuffIter->TarPosY());

			if(StuffIter->Target() == m_vMenuItems[0].Target())
			{
				StuffIter->GoTo(fTempX, fTempY);
				break;
			}
		}
	}
	
	if(	CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_UP) || 
		CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_LEFT) ||
		((CXInput::GetInstance()->LStickY() >= 7850 ||
		CXInput::GetInstance()->LStickX() <= -7850) && CXInput::GetInstance()->IsLStickMoved() == false))
	{
		m_fIntroCounter = 0;
		CSGD_FModManager::GetInstance()->PlaySound(CGame::GetInstance()->ChoiceSound());
		CSGD_FModManager::GetInstance()->SetVolume(CGame::GetInstance()->ChoiceSound(), CGame::GetInstance()->SFXVol());
		CXInput::GetInstance()->IsLStickMoved(true);
		vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();
		float fTempX = StuffIter->TarPosX();
		float fTempY = StuffIter->TarPosY();

		while(StuffIter != m_vMenuItems.end())
		{
			vector<CMenuItem>::iterator temp = StuffIter;

			StuffIter++;

			temp->GoTo(StuffIter->TarPosX(), StuffIter->TarPosY());

			if(StuffIter->Target() == m_vMenuItems[m_vMenuItems.size()-1].Target())
			{
				StuffIter->GoTo(fTempX, fTempY);
				break;
			}
		}
	}
	if((CXInput::GetInstance()->LStickY() > -7850 && CXInput::GetInstance()->LStickY() < 7850 &&
		CXInput::GetInstance()->LStickX() < 7850 &&	CXInput::GetInstance()->LStickX() > -7850)
		&& CXInput::GetInstance()->IsLStickMoved() == true)
	{
		CXInput::GetInstance()->IsLStickMoved(false);
	}

	/////////////////////////////////////////////////////////////////
	// BUG FIX
	// Reference Bug # BB-002
	// Approved by:
	// BUG FIX START
	/////////////////////////////////////////////////////////////////

	if(	((CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_RETURN)) && (CSGD_DirectInput::GetInstance()->KeyUp(DIK_LALT))) ||
		CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_A && CXInput::GetInstance()->GetIsADown() == false)
	{
		m_fIntroCounter = 0;
		///////////////////////////////////////////////////////////////////////////
		// BUG FIX END  Reference # BB-002
		//////////////////////////////////////////////////////////////////////////
		
		CSGD_FModManager::GetInstance()->PlaySound(CGame::GetInstance()->ChosenSound());
		CSGD_FModManager::GetInstance()->SetVolume(CGame::GetInstance()->ChosenSound(), CGame::GetInstance()->SFXVol());
		CXInput::GetInstance()->SetIsADown(true);
		vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();

		while(StuffIter != m_vMenuItems.end())
		{
			if(StuffIter->TarPosY() == 200)
			{
				if(StuffIter->Name() == "Exit")
				{
					CGame::GetInstance()->ChangeState(NULL);
					return false;
				}
				else if(StuffIter->Target()->Type() == SUB)
					CGame::GetInstance()->PushState(StuffIter->Target());
				else
					CGame::GetInstance()->ChangeState(StuffIter->Target());
				return true;
			}
			++StuffIter;
		}
	}
	else if(!(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_A) && CXInput::GetInstance()->GetIsADown() == true)
		CXInput::GetInstance()->SetIsADown(false);
	return true;
}
void CMainMenuState::Enter()
{
	m_bfItem.DrawBitmapText("Loading...", 200, 200, 2.0f);
	/////////////////////////////////////////////////
	//	Menu Items
	CMenuItem miPlay;
	miPlay.Init(&m_bfItem, 200, 0, "Play", CGamePlayState::GetInstance(), 1.0f);
	miPlay.GoTo(50, 200);
	m_vMenuItems.push_back(miPlay);
	CMenuItem miOptions;
	miOptions.Init(&m_bfItem, 200, 0, "Options", COptionMenuState::GetInstance(), 1.0f);
	miOptions.GoTo(50, 250);
	m_vMenuItems.push_back(miOptions);
	CMenuItem miPoo;
	miPoo.Init(&m_bfItem, 200, 0, "How To Play", CHowToPlayState::GetInstance(), 1.0f);
	miPoo.GoTo(50, 300);
	m_vMenuItems.push_back(miPoo);
	CMenuItem miCred;
	miCred.Init(&m_bfItem, 200, 0, "Credits", CCreditState::GetInstance(), 1.0f);
	miCred.GoTo(50, 350);
	m_vMenuItems.push_back(miCred);
	CMenuItem miExit;
	miExit.Init(&m_bfItem, 200, 0, "Exit", CExitState::GetInstance(), 1.0f);
	miExit.GoTo(50, 400);
	m_vMenuItems.push_back(miExit);
	//
	/////////////////////////////////////////////////

	srand(unsigned int (time(0)));

	if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nBGMusic))
	{
		CSGD_FModManager::GetInstance()->PlaySound(m_nBGMusic);
		CSGD_FModManager::GetInstance()->SetVolume(m_nBGMusic, CGame::GetInstance()->MusicVol());
	}

	CGamePlayState::GetInstance()->SecondLevel(false);

	ObjectGenerator<string, CBase>::GetInstance()->RegisterClassType<CUnit>("CUnit");
	MailSystem::GetInstance()->InitMessageSystem(CGame::GetInstance()->MessageProc);

	for(int i = 0; i < 100; ++i)
		MailSystem::GetInstance()->SendMail(new SpawnUnitMenu());

	
	MailSystem::GetInstance()->ReadMail();

	m_fIntroCounter = 0;
}
void CMainMenuState::Exit()
{
	m_vMenuItems.clear();

	if(CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nBGMusic))
		CSGD_FModManager::GetInstance()->StopSound(m_nBGMusic);

	MailSystem::GetInstance()->ShutdownMailSystem();
	ObjectGenerator<string, CBase>::GetInstance()->UnregisterClassType("CUnit");
	ObjectGenerator<string, CBase>::GetInstance()->ShutdownObjectFactory();
	CObjectManager::GetInstance()->Clear();

}
void CMainMenuState::Update(float fDelta)
{
	if(fDelta <=1)
		m_fIntroCounter += fDelta;
	if(m_fIntroCounter >= 10.0f)
		CGame::GetInstance()->ChangeState(CIntroState::GetInstance());
	vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();

	while(StuffIter != m_vMenuItems.end())
	{
		StuffIter->Update(fDelta);
		++StuffIter;
	}

	MailSystem::GetInstance()->ReadMail();
	CEventSystem::GetInstance()->ProcessEvents();
	CObjectManager::GetInstance()->Update(fDelta);
}
void CMainMenuState::Render()
{	
	/////////////////////////////////////////////////////////////////
	// BUG FIX
	// Reference Bug # BB-001 & BB-023
	// Approved by: 
	// BUG FIX START
	/////////////////////////////////////////////////////////////////
	CSGD_TextureManager::GetInstance()->Draw(m_nBGImage,0, 0, .8f, .6f);
	///////////////////////////////////////////////////////////////////////////
	// BUG FIX END  Reference # BB-001 & BB-023
	//////////////////////////////////////////////////////////////////////////

	CObjectManager::GetInstance()->Render();

	m_bfItem.DrawBitmapText("Main Menu", 25, 100, 2.0f);
	
	vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();

	while(StuffIter != m_vMenuItems.end())
	{
		if(StuffIter->TarPosY() == 200)
			StuffIter->Render(true);
		else
			StuffIter->Render();

		++StuffIter;
	}
	

}